#ifndef _CACHEMANAGER_H_
#define _CACHEMANAGER_H_

#include <iostream>
#include <string>
#include <vector>
#include "LRUCache.h"
using std::string;
using std::vector;

class CacheManager
{
private:
    CacheManager();
    ~CacheManager(){};
    CacheManager(CacheManager &) = delete;
    CacheManager &operator=(CacheManager &) = delete;
    static pthread_once_t _ptht;
    static CacheManager *_pinstance;
    //缓存队列，数量应与线程个数一致，4个
    vector<LRUCache> _cacheList;

public:
    static void destroy()
    {
        if(_pinstance!=nullptr)
        {
            delete _pinstance;
            _pinstance = nullptr;
        }
    }
    static void init()
    {
        if(_pinstance==nullptr)
        {
            _pinstance = new CacheManager();
        }
        atexit(destroy);
    }

    static CacheManager* getInstance()
    {
        pthread_once(&_ptht, &init);
        return _pinstance;
    }
    //从磁盘读取缓存文件
    void initCache(size_t num, const string &filename);
    //获取某个缓存队列,参数为下标
    LRUCache &getCache(size_t idx);
    //定时更新所有缓存,下标为0的为主缓存
    void updateAllCaches();
};

#endif